//
//  PlayerHomeViewController.m
//  FNMenu
//
//  Created by 航汇聚科技 on 2018/11/1.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "PlayerHomeViewController.h"
#import "NSDictionary+Safety.h"
#import "NSArray+Safety.h"
#import "SongViewCell.h"
#import "PlayerManager.h"
@interface PlayerHomeViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *songsTableVIew;
@property (nonatomic, strong) NSArray *songs;
@end

@implementation PlayerHomeViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.songs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SongViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"songs" forIndexPath:indexPath];
    NSDictionary *songsInfo = [self.songs safeObjectAtIndex:indexPath.row];
    cell.songsNameLabel.text = [songsInfo safeObjectForKey:@"song"];
    cell.singerNameLabel.text = [songsInfo safeObjectForKey:@"singer"];
    if ([[PlayerManager defaultPlayer].currentPlaySongName isEqualToString:[songsInfo safeObjectForKey:@"song"]]) {
        cell.songsNameLabel.textColor = [UIColor purpleColor];
        cell.singerNameLabel.textColor = [UIColor purpleColor];
        cell.songsPlayStateControlButton.hidden = NO;
        cell.songsPlayStateControlButton.userInteractionEnabled = YES;
        if ([PlayerManager defaultPlayer].isPlaying) {
            [cell.songsPlayStateControlButton setImage:[UIImage imageNamed:@"play_playing"] forState:UIControlStateNormal];
        }else {
            [cell.songsPlayStateControlButton setImage:[UIImage imageNamed:@"play_paused"] forState:UIControlStateNormal];
        }
    }else {
        cell.songsNameLabel.textColor = [UIColor darkGrayColor];
        cell.singerNameLabel.textColor = [UIColor lightGrayColor];
        cell.songsPlayStateControlButton.hidden = YES;
        cell.songsPlayStateControlButton.userInteractionEnabled = NO;
    }
    [cell.songsPlayStateControlButton addTarget:self action:@selector(playOrPause) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SongViewCell *cell = (SongViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    [[PlayerManager defaultPlayer] playWithName:cell.songsNameLabel.text];
    [self.songsTableVIew reloadData];
}
#pragma mark - event response
- (IBAction)dismissVC:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)playOrPause {
    [[PlayerManager defaultPlayer] pausedOrContinuePlay];
    [self.songsTableVIew reloadData];
}
#pragma mark - public
+ (instancetype)playerHomeVCFromStoryboard {
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PlayerHome"];
}
#pragma mark - getter
- (NSArray *)songs {
    if (!_songs) {
        _songs = [PlayerManager defaultPlayer].songs;
    }
    return _songs;
}
@end
